from flask import Blueprint, request
from app.utils import json
from db import db
from db.models import UserFeedback

feedback_api = Blueprint('feedback_api', __name__)


# 查看反馈（喜欢/不喜欢列表）
@feedback_api.route('/v1/feedbackList', methods=['GET'])
def feedback_list():
    device_id = request.args.get('device_id')
    if device_id is None:
        return json(400, 'device_id is required', '')
    kind = request.args.get('type')
    if kind is None:
        return json(400, 'type is required', '')
    user_feedback = UserFeedback.query.filter_by(device_id=device_id, type=kind).all()
    user_feedback_list = []
    for feedback in user_feedback:
        user_feedback_list.append({
            'id': feedback.id,
            'device_id': feedback.device_id,
            'type': kind,
            'food_name': feedback.food_name,
            'food_desc': feedback.food_desc,
            'food_level': feedback.food_level,
            'created_at': feedback.created_at.strftime('%Y-%m-%d %H:%M:%S')
        })

    return json(200, 'success', user_feedback_list)


# 设置反馈（喜欢/不喜欢）
@feedback_api.route('/v1/setFeedback', methods=['POST'])
def set_feedback():
    device_id = request.json.get('device_id')
    if device_id is None:
        return json(400, 'device_id is required', '')
    kind = request.json.get('type')
    if kind is None:
        return json(400, 'type is required', '')
    food_name = request.json.get('food_name')
    if food_name is None:
        return json(400, 'food_name is required', '')
    food_desc = request.json.get('food_desc')
    if food_desc is None:
        return json(400, 'food_desc is required', '')
    food_level = request.json.get('food_level')
    if food_level is None:
        return json(400, 'food_level is required', '')

    feedback = UserFeedback.query.filter_by(device_id=device_id, type=kind, food_name=food_name).first()
    if feedback is not None:
        # 说明已经存在了，直接返回成功
        return json(200, 'success', '')
    else:
        feedback = UserFeedback(device_id=device_id, type=kind, food_name=food_name, food_desc=food_desc,
                                food_level=food_level)
        db.session.add(feedback)
        db.session.commit()

    return json(200, 'success', '')


# 取消反馈
@feedback_api.route('/v1/cancelFeedback', methods=['POST'])
def cancel_feedback():
    id = request.json.get('id')
    if id <= 0:
        return json(400, 'id is required', '')
    device_id = request.json.get('device_id')
    if device_id is None:
        return json(400, 'device_id is required', '')

    feedback = UserFeedback.query.filter_by(id=id).first()
    if feedback is None:
        return json(400, 'data is null', '')
    db.session.delete(feedback)
    db.session.commit()

    return json(200, 'success', '')


# 通过设备id获取反馈
def get_feedback_by_device_id(device_id, kind):
    feedback_ls = UserFeedback.query.filter_by(device_id=device_id, type=kind).all()
    names = []
    for feedback in feedback_ls:
        names.append(feedback.food_name)
    return names
